package pers.crayon.baseprogram.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.crayon.baseprogram.util.FileUtil;

import java.io.IOException;

/**
 * 用于演示 tomcat配置与多线程调用
 *
 * @author wangzg13
 * @since 2022/9/13
 */
@Slf4j
@RestController
public class DemoController {

    // 第一次
    ThreadLocal<Integer> count = new ThreadLocal();

    public DemoController() {
        this.count.set(0);
    }


    @PostMapping("/test")
    public String test() {
        try {
            // 线程休眠3000ms,一个请求正常响应为3000+几十ms，程序预热后更快
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
        return "ok";
    }

    /**
     * 获取当前应用配置文件内容
     * 不以  @ConfigurationProperties() 来指定，而是全部输出
     *
     * @return
     */
    @GetMapping("/getProperties")
    public String getProperties(@RequestParam String fileName) throws IOException {
        String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
        log.info(path);
        String filePath = path + fileName;
        log.info(filePath);
        return FileUtil.getFileContent(filePath);
    }
}
